from math import ceil
from math import floor
from typing import List


class Solution:
    def circleGame(self, toys: List[List[int]], circles: List[List[int]], r: int) -> int:
        circles = {(x0, y0) for x0, y0 in circles}

        ans = 0

        # 遍历每个玩具
        for x0, y0, r0 in toys:
            # 两个圆心的最大距离
            distance = (r - r0)

            # print("玩具:", (x0, y0, r0), "->", "最大距离:", distance)

            # 考虑所有可能的圆心的x坐标
            x_min = x0 - distance
            x_max = x0 + distance

            distance_square = pow(distance, 2)

            already_find = False

            for x1 in range(x_min, x_max + 1, 1):
                # 当前圆心的x坐标在y轴上的最远距离
                y_distance = pow(distance_square - pow((x0 - x1), 2), 0.5)

                # print(x1, ":", y0 - y_distance, y0 + y_distance)

                # 考虑所有可能的圆心的y坐标
                y_min = ceil(y0 - y_distance)
                y_max = floor(y0 + y_distance)

                for y1 in range(y_min, y_max + 1, 1):
                    # print(x1, y1)
                    if (x1, y1) in circles:
                        ans += 1
                        already_find = True
                        break

                if already_find:
                    break

        return ans


if __name__ == "__main__":
    # 1
    print(Solution().circleGame(toys=[[3, 3, 1], [3, 2, 1]],
                                circles=[[4, 3]],
                                r=2))

    # 2
    print(Solution().circleGame(toys=[[1, 3, 2], [4, 3, 1], [7, 1, 2]],
                                circles=[[1, 0], [3, 3]],
                                r=4))

    # 自制用例
    # 1
    print(Solution().circleGame(toys=[[3, 4, 1]],
                                circles=[[3, 5], [4, 6]],
                                r=6))
